<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/default}">
<head>
    <title>新增联系人</title>
</head>
<body>
    <div layout:fragment="content">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h1>新增联系人</h1>
            <a th:href="@{/contact/list}" class="btn btn-secondary">返回列表</a>
        </div>
        
        <div class="card">
            <div class="card-body">
                <form id="contactForm" data-validate="true" data-redirect="/crm/contact/list">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="name">姓名</label>
                                <input type="text" class="form-control" id="name" name="name" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="phone">电话</label>
                                <input type="text" class="form-control" id="phone" name="phone" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="email">电子邮箱</label>
                                <input type="email" class="form-control" id="email" name="email">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="position">职位</label>
                                <input type="text" class="form-control" id="position" name="position">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="customerId">所属客户</label>
                                <select class="form-control" id="customerId" name="customerId" required>
                                    <option value="">请选择客户</option>
                                    <!-- 这里需要动态加载客户列表 -->
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>是否主要联系人</label>
                                <div>
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="radio" name="isPrimary" id="isPrimaryYes" value="true">
                                        <label class="form-check-label" for="isPrimaryYes">是</label>
                                    </div>
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="radio" name="isPrimary" id="isPrimaryNo" value="false" checked>
                                        <label class="form-check-label" for="isPrimaryNo">否</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label for="remark">备注</label>
                                <textarea class="form-control" id="remark" name="remark" rows="3"></textarea>
                            </div>
                        </div>
                    </div>
                    
                    <div class="text-right">
                        <button type="submit" class="btn btn-primary">保存</button>
                        <a th:href="@{/contact/list}" class="btn btn-secondary ml-2">取消</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <th:block layout:fragment="scripts">
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                // 加载客户列表
                fetch('/crm/api/customer/list?size=100')
                    .then(response => response.json())
                    .then(result => {
                        if (result.code === 200 && result.data && result.data.records) {
                            const select = document.getElementById('customerId');
                            
                            result.data.records.forEach(customer => {
                                const option = document.createElement('option');
                                option.value = customer.id;
                                option.textContent = customer.name;
                                select.appendChild(option);
                            });
                        }
                    })
                    .catch(error => {
                        console.error('加载客户列表失败:', error);
                    });
                
                // 初始化表单提交
                submitForm('contactForm', '/crm/api/contact/add');
            });
        </script>
    </th:block>
</body>
</html> 